/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.config.client.widget;

import java.util.ArrayList;
import java.util.List;

import net.carefx.framework.container.client.constant.FrameworkConstants;
import net.carefx.framework.container.client.context.AppContext;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.framework.dict.web.gwt.client.DictComboBox;
import com.th.supcom.framework.dict.web.gwt.client.model.DictModel;
import com.th.supcom.framework.organization.web.gwt.client.OrgComboBox;
import com.th.supcom.framework.organization.web.gwt.client.model.OrganizationModel;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.config.client.common.PdsConfigConstant;
import com.th.supcom.pds.config.client.common.PdsConfigEnableEnum;
import com.th.supcom.pds.config.client.common.PdsConfigExistEnum;
import com.th.supcom.pds.config.client.common.PdsConfigRangeEnum;
import com.th.supcom.pds.config.client.common.PhamConfigServiceFactory;
import com.th.supcom.pds.config.client.common.PhamDispensaryConfigModuleEvents;
import com.th.supcom.pds.config.client.model.PhamDispensaryConfigModel;
import com.th.supcom.pds.config.client.service.PhamDispensaryConfigRpcAsync;

/**
 * 新增或编辑药房属性
 * 
 * @author wangjian
 */
public class EditPhamDispensaryConfigWindow extends Window
{
	private FormPanel addPhamConfigForm;
    private static final PhamDispensaryConfigRpcAsync s_PHAM_DISPENSARY_CONFIG_ASYNC = PhamConfigServiceFactory.getPhamDispensaryConfigService ();
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    private Button m_addButton;
    /** 全院 */
    private Radio m_all;
    private Button m_cancelButton;
    /** 煎药机连接 */
    private RadioGroup m_decotionFlag;
    /** 出库类型 */
    private DictComboBox m_deliveryType;
    private Radio m_disableRadio;
    /** 所属药房 */
    private OrgComboBox m_dispensary;
    /** 摆药单打印标识 */
    private RadioGroup m_dispensePrintFlag;
    private Radio m_enableRadio;
    /** 启用类型 */
    private RadioGroup m_enableType;
    /** 开放结束时间 */
    private TimeCombox m_endTime;
    /** 静配配置 */
    private RadioGroup m_infusionConfig;
    /** 静配开放标识 */
    private RadioGroup m_infusionFlag;

    private PhamDispensaryConfigModel m_model = new PhamDispensaryConfigModel ();
    private Radio m_negativeConn;
    private Radio m_negativeInfuFlag;
    private Radio m_negativeInfusion;
    private Radio m_negativeQueneExist;
    private Radio m_noPrint;
    /** 摆药类型 */
    private DictComboBox m_phamType;
    private Radio m_positiveConn;
    private Radio m_positiveInfuFlag;
    private Radio m_positiveInfusion;
    private Radio m_positiveQueneExist;
    /** 处方配置日期 */
    private NumberField m_prescDate;
    private Radio m_print;
    /** 排队系统提示类型 */
    private DictComboBox m_queneAlertType;
    private Radio m_queneClose;
    /** 排队系统配置 */
    private RadioGroup m_queneConfig;

    /** 排队系统开放标识 */
    private RadioGroup m_queneFlag;

    private Radio m_queneOpen;

    /** 处方查询范围 */
    private RadioGroup m_searchRange;

    /** 自身 */
    private Radio m_self;

    /** 班次类型 */
    private DictComboBox m_shiftType;

    /** 开放起始时间 */
    private SimpleComboBox <String> m_startTime;

    private final List <Component> m_widgetList = new ArrayList <Component> ();

    public EditPhamDispensaryConfigWindow ()
    {
        initUI ();
        registerListeners ();
    }

    public EditPhamDispensaryConfigWindow (final PhamDispensaryConfigModel model)
    {
        this.m_model = model;
        initUI ();
        registerListeners ();
    }

    /**
     * 添加左侧radio
     * 
     * @param left
     * @param formData
     */
    private void addLeftRadio (final LayoutContainer left, final FormData formData)
    {
        m_infusionConfig = new RadioGroup ();
        m_infusionConfig.setFieldLabel ("静脉输液配置");

        m_positiveInfusion = new Radio ();
        m_positiveInfusion.setBoxLabel ("有");
        m_positiveInfusion.setValueAttribute (PdsConfigExistEnum.Exist.getCode () + "");
        m_positiveInfusion.setValue (true);

        m_negativeInfusion = new Radio ();
        m_negativeInfusion.setBoxLabel ("无");
        m_negativeInfusion.setTabIndex (9);
        m_negativeInfusion.setValueAttribute (PdsConfigExistEnum.NotExist.getCode () + "");
        m_negativeInfusion.setValue (false);

        m_infusionConfig.add (m_positiveInfusion);
        m_infusionConfig.add (m_negativeInfusion);
        m_infusionConfig.setValue (m_negativeInfusion);
        left.add (m_infusionConfig, formData);

        m_decotionFlag = new RadioGroup ();
        m_decotionFlag.setFieldLabel ("连接煎药机");

        m_positiveConn = new Radio ();
        m_positiveConn.setBoxLabel ("是");
        m_positiveConn.setValueAttribute (PdsConfigEnableEnum.Enable.getCode () + "");
        m_positiveConn.setValue (true);

        m_negativeConn = new Radio ();
        m_negativeConn.setBoxLabel ("否");
        m_negativeConn.setValueAttribute (PdsConfigEnableEnum.Disable.getCode () + "");
        m_negativeConn.setTabIndex (11);
        m_negativeConn.setValue (false);

        m_decotionFlag.add (m_positiveConn);
        m_decotionFlag.add (m_negativeConn);
        m_decotionFlag.setValue (m_negativeConn);
        left.add (m_decotionFlag, formData);

        m_queneConfig = new RadioGroup ();
        m_queneConfig.setFieldLabel ("排队系统");

        m_positiveQueneExist = new Radio ();
        m_positiveQueneExist.setBoxLabel ("有");
        m_positiveQueneExist.setValueAttribute (PdsConfigExistEnum.Exist.getCode () + "");
        m_positiveQueneExist.setTabIndex (13);
        m_positiveQueneExist.setValue (false);

        m_negativeQueneExist = new Radio ();
        m_negativeQueneExist.setBoxLabel ("无");
        m_negativeQueneExist.setValueAttribute (PdsConfigExistEnum.NotExist.getCode () + "");
        m_negativeQueneExist.setValue (false);

        m_queneConfig.add (m_positiveQueneExist);
        m_queneConfig.add (m_negativeQueneExist);
        m_queneConfig.setValue (m_positiveQueneExist);
        left.add (m_queneConfig, formData);

    }

    /**
     * 添加右侧radio
     * 
     * @param right
     * @param formData
     */
    private void addRightRadio (final LayoutContainer right, final FormData formData)
    {
        m_searchRange = new RadioGroup ();
        m_searchRange.setFieldLabel ("处方查询范围");

        m_self = new Radio ();
        m_self.setBoxLabel ("本科室");
        m_self.setTabIndex (10);
        m_self.setValueAttribute (PdsConfigRangeEnum.Self.getCode () + "");
        m_self.setValue (true);

        m_all = new Radio ();
        m_all.setBoxLabel ("全院");
        m_all.setValueAttribute (PdsConfigRangeEnum.All.getCode () + "");
        m_all.setValue (false);

        m_searchRange.add (m_self);
        m_searchRange.add (m_all);
        m_searchRange.setValue (m_self);
        right.add (m_searchRange, formData);

        m_infusionFlag = new RadioGroup ();
        m_infusionFlag.setFieldLabel ("静配开放");

        m_positiveInfuFlag = new Radio ();
        m_positiveInfuFlag.setBoxLabel ("是");
        m_positiveInfuFlag.setValueAttribute (PdsConfigEnableEnum.Enable.getCode () + "");
        m_positiveInfuFlag.setValue (true);

        m_negativeInfuFlag = new Radio ();
        m_negativeInfuFlag.setBoxLabel ("否");
        m_negativeInfuFlag.setValueAttribute (PdsConfigEnableEnum.Disable.getCode () + "");
        m_negativeInfuFlag.setTabIndex (12);
        m_negativeInfuFlag.setValue (false);

        m_infusionFlag.add (m_positiveInfuFlag);
        m_infusionFlag.add (m_negativeInfuFlag);
        m_infusionFlag.setValue (m_negativeInfuFlag);
        right.add (m_infusionFlag, formData);

        m_dispensePrintFlag = new RadioGroup ();
        m_dispensePrintFlag.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "打印摆药单");

        m_print = new Radio ();
        m_print.setBoxLabel ("是");
        m_print.setValueAttribute (PdsConfigEnableEnum.Enable.getCode () + "");
        m_print.setTabIndex (14);
        m_print.setValue (true);

        m_noPrint = new Radio ();
        m_noPrint.setBoxLabel ("否");
        m_noPrint.setValueAttribute (PdsConfigEnableEnum.Disable.getCode () + "");
        m_noPrint.setValue (false);

        m_dispensePrintFlag.add (m_print);
        m_dispensePrintFlag.add (m_noPrint);
        m_dispensePrintFlag.setValue (m_print);
        right.add (m_dispensePrintFlag, formData);

        m_queneFlag = new RadioGroup ();
        m_queneFlag.setFieldLabel ("排队系统开放");

        m_queneOpen = new Radio ();
        m_queneOpen.setBoxLabel ("是");
        m_queneOpen.setValueAttribute (PdsConfigEnableEnum.Enable.getCode () + "");
        m_queneOpen.setTabIndex (16);
        m_queneOpen.setValue (true);

        m_queneClose = new Radio ();
        m_queneClose.setBoxLabel ("否");
        m_queneClose.setValueAttribute (PdsConfigEnableEnum.Disable.getCode () + "");
        m_queneClose.setValue (false);

        m_queneFlag.add (m_queneOpen);
        m_queneFlag.add (m_queneClose);
        m_queneFlag.setValue (m_queneOpen);
        right.add (m_queneFlag, formData);

    }

    private PhamDispensaryConfigModel getSaveData ()
    {
        m_model.setPhamDispensaryCode (m_dispensary.getValue ().getCode ());
        m_model.setPhamType (m_phamType.getValue ().getDictItemCode ());
        m_model.setShiftType (m_shiftType.getValue ().getDictItemCode ());
        m_model.setStartTime (m_startTime.getValue ().getValue ());
        m_model.setEndTime (m_endTime.getValue ().getValue ());
        m_model.setPrescDateBefore (m_prescDate.getValue ().intValue ());
        m_model.setOpenFlag (Integer.parseInt (m_enableType.getValue ().getValueAttribute ()));
        m_model.setSearchRange (Integer.parseInt (m_searchRange.getValue ().getValueAttribute ()));
        m_model.setDispensePrintFlag (Integer.parseInt (m_dispensePrintFlag.getValue ().getValueAttribute ()));
        m_model.setInfusionConfig (Integer.parseInt (m_infusionConfig.getValue ().getValueAttribute ()));
        m_model.setInfusionOpen (Integer.parseInt (m_infusionFlag.getValue ().getValueAttribute ()));
        m_model.setDecoteConnFlag (Integer.parseInt (m_decotionFlag.getValue ().getValueAttribute ()));
        m_model.setQueueConfig (Integer.parseInt (m_queneConfig.getValue ().getValueAttribute ()));
        m_model.setQueueEnable (Integer.parseInt (m_queneFlag.getValue ().getValueAttribute ()));
        m_model.setDeliveryType (m_deliveryType.getValue ().getDictItemCode ());
        if (null != m_queneAlertType.getValue ())
        {
            m_model.setQueueType (m_queneAlertType.getValue ().getDictItemCode ());
        }
        m_model.setDeptPinyinCode (m_dispensary.getValue ().getPinyinCode ());
        return m_model;
    }

    private void initialInfusionRadio (final PhamDispensaryConfigModel result)
    {
        m_infusionConfig.setValue (result.getInfusionConfig () == PdsConfigExistEnum.Exist.getCode () ? m_positiveInfusion
                                                                                                     : m_negativeInfusion);
        m_infusionFlag.setValue (result.getInfusionOpen () == PdsConfigEnableEnum.Enable.getCode () ? m_positiveInfuFlag
                                                                                                   : m_negativeInfuFlag);
    }

    private void initialLeftRadio (final PhamDispensaryConfigModel result)
    {
        m_enableType.setValue (result.getOpenFlag () == PdsConfigEnableEnum.Enable.getCode () ? m_enableRadio
                                                                                             : m_disableRadio);
        m_dispensePrintFlag.setValue (result.getDispensePrintFlag () == PdsConfigEnableEnum.Enable.getCode () ? m_print
                                                                                                             : m_noPrint);
        m_decotionFlag.setValue (result.getDecoteConnFlag () == PdsConfigEnableEnum.Enable.getCode () ? m_positiveConn
                                                                                                     : m_negativeConn);
    }

    private void initialRightRadio (final PhamDispensaryConfigModel result)
    {
        initialInfusionRadio (result);
        m_queneConfig.setValue (result.getQueueConfig () == PdsConfigExistEnum.Exist.getCode () ? m_positiveQueneExist
                                                                                               : m_negativeQueneExist);
        m_queneFlag.setValue (result.getQueueEnable () == PdsConfigEnableEnum.Enable.getCode () ? m_queneOpen
                                                                                               : m_queneClose);
        m_searchRange.setValue (result.getSearchRange () == PdsConfigRangeEnum.Self.getCode () ? m_self : m_all);
    }

    private void initLeftContainer (final LayoutContainer left)
    {
        final FormData formData = new FormData ("100%");
        left.setBorders (false);
        left.setStyleAttribute ("paddingRight", "18px");
        final FormLayout layout = new FormLayout ();
        layout.setLabelWidth (100);
        layout.setLabelAlign (LabelAlign.RIGHT);
        left.setLayout (layout);

        m_dispensary = new OrgComboBox ();
        m_dispensary.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "药房");
        m_dispensary.setTabIndex (1);
        final OrganizationModel orgModel = new OrganizationModel ();
        if(PhamConfigWidgetUtils.getIsGlobal()==0){
        	 orgModel.setCode (ClientUtils.getOrgModel ().getOrgCode ());
             orgModel.setName (ClientUtils.getOrgModel ().getOrgName ());
             m_dispensary.setEnabled(false);
        }
        m_dispensary.setValue (orgModel); 
        
        m_dispensary.setAllowBlank (false);
        m_dispensary.setEmptyText ("请选择");
        left.add (m_dispensary, formData);

        m_shiftType = new DictComboBox (PdsConfigConstant.SHIFT_TYPE_DICT_CODE);
        m_shiftType.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "班次类型");
        m_shiftType.setTabIndex (3);
        m_shiftType.setAllowBlank (false);
        m_shiftType.setEmptyText ("请选择");
        left.add (m_shiftType, formData);

        m_startTime =  new TimeCombox();
        m_startTime.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "起始时间");
        m_startTime.setTabIndex (5);
        m_startTime.setAllowBlank (false);
        m_startTime.setEmptyText ("请选择");
        left.add (m_startTime, formData);

        m_prescDate = new NumberField ();
        m_prescDate.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "处方日期");
        m_prescDate.setRegex ("^[0-9]*[1-9][0-9]*$");
        m_prescDate.setPropertyEditorType (Integer.class);
        m_prescDate.setAllowBlank (false);
        m_prescDate.setEmptyText ("查询配置日期到当前日期的处方");
        m_prescDate.setMaxLength (3);
        m_prescDate.setTabIndex (7);
        left.add (m_prescDate, formData);

        addLeftRadio (left, formData);

        m_queneAlertType = new DictComboBox (PdsConfigConstant.QUENE_TYPE_DICT_CODE);
        m_queneAlertType.setFieldLabel ("排队提示类型");
        m_queneAlertType.setTabIndex (15);
        m_queneAlertType.setEmptyText ("请选择");
        left.add (m_queneAlertType, formData);
    }

    private void initRightContainer (final LayoutContainer right)
    {
        final FormData formData = new FormData ("100%");
        right.setStyleAttribute ("paddingRight", "18px");
        final FormLayout layout = new FormLayout ();
        layout.setLabelWidth (100);
        layout.setLabelAlign (LabelAlign.RIGHT);
        right.setLayout (layout);

        m_phamType = new DictComboBox (PdsConfigConstant.DISPENSE_TYPE_DICT_CODE);
        
        m_phamType.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "摆药类型");
        m_phamType.setTabIndex (2);
        m_phamType.setAllowBlank (false);
        m_phamType.setEmptyText ("请选择");
        right.add (m_phamType, formData);

        m_enableType = new RadioGroup ();
        m_enableRadio = new Radio ();
        m_disableRadio = new Radio ();
        m_enableRadio.setTabIndex (4);
        PhamConfigWidgetUtils.generateEnableRadio (m_enableType, m_enableRadio, m_disableRadio, "药房");

        right.add (m_enableType, formData);

        m_endTime = new TimeCombox ();
        m_endTime.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "结束时间");
        m_endTime.setTabIndex (6);
        m_endTime.setAllowBlank (false);
        m_endTime.setEmptyText ("请选择");
        right.add (m_endTime, formData);

        m_deliveryType = new DictComboBox (PdsConfigConstant.DELIVERY_TYPE_CODE);
        m_deliveryType.setFieldLabel (FrameworkConstants.FORM_MUST_FILL_TIP + "出库类型");
        m_deliveryType.setTabIndex (8);
        m_deliveryType.setEmptyText ("请选择");
        m_deliveryType.setAllowBlank (false);
        right.add (m_deliveryType, formData);

        addRightRadio (right, formData);
    }

    /**
     * 初始化UI控件.
     * 
     */
    private void initUI ()
    {
        // 初始化form panel
        setSize (600, 300);
        setBlinkModal (true);
        setModal (true);
        setResizable (false);

        setButtonAlign (HorizontalAlignment.RIGHT);
        setLayout (new FitLayout ());

        addPhamConfigForm = new FormPanel ();
        addPhamConfigForm.setFrame (false);
        addPhamConfigForm.setBodyBorder (false);
        addPhamConfigForm.setBorders (false);
        addPhamConfigForm.setHeaderVisible (false);
        final FormLayout layout = new FormLayout ();
        layout.setLabelWidth (95);
        layout.setLabelAlign (LabelAlign.RIGHT);
        addPhamConfigForm.setLayout (layout);

        final LayoutContainer main = new LayoutContainer ();
        main.setLayout (new ColumnLayout ());

        // 初始化左布局容器
        final LayoutContainer left = new LayoutContainer ();
        initLeftContainer (left);

        // 初始化右布局容器
        final LayoutContainer right = new LayoutContainer ();
        initRightContainer (right);

        main.add (left, new ColumnData (.5));
        main.add (right, new ColumnData (.5));

        final FormData formData = new FormData ("100%");
        addPhamConfigForm.add (main, formData);

        add (addPhamConfigForm);
        m_addButton = new Button ("确定(O)");
        m_addButton.setTabIndex (16);
        m_cancelButton = new Button ("取消(C)");
        m_cancelButton.setTabIndex (17);
        if(PhamConfigWidgetUtils.getIsGlobal ()==0){
            m_cancelButton.hide ();
        }
        final FormButtonBinding buttonBinding = new FormButtonBinding (addPhamConfigForm);
        buttonBinding.addButton (m_addButton);
        addButton (m_addButton);
        addButton (m_cancelButton);

        m_widgetList.add (m_dispensary);
        m_widgetList.add (m_phamType);
        m_widgetList.add (m_shiftType);
        m_widgetList.add (m_enableRadio);
        m_widgetList.add (m_startTime);
        m_widgetList.add (m_endTime);
        m_widgetList.add (m_prescDate);
        m_widgetList.add (m_self);
        m_widgetList.add (m_positiveInfusion);
        m_widgetList.add (m_deliveryType);
        m_widgetList.add (m_positiveInfuFlag);
        m_widgetList.add (m_positiveConn);
        m_widgetList.add (m_print);
        m_widgetList.add (m_positiveQueneExist);
        m_widgetList.add (m_queneOpen);
        m_widgetList.add (m_queneAlertType);
        m_widgetList.add (m_addButton);
    }

    private void keyEnterListener ()
    {
        for (int i = 0; i < m_widgetList.size () - 1; i++)
        {
            final int index = i;
            m_widgetList.get (i).addListener (Events.KeyUp, new Listener <ComponentEvent> ()
            {
                @Override
                public void handleEvent (final ComponentEvent event)
                {
                    if (KeysConstants.VK_ENTER == event.getKeyCode ())
                    {
                        m_widgetList.get (index + 1).focus ();
                        return;
                    }
                }
            });
        }
    }

    @Override
    protected void onRender (final Element parent, final int pos)
    {
        super.onRender (parent, pos);
        //setFocusWidget (m_dispensary);
       
        if (null != m_model.getPhamDispensaryConfigId ())
        {
            setFocusWidget (m_phamType);
            EditPhamDispensaryConfigWindow.s_PHAM_DISPENSARY_CONFIG_ASYNC.getPhamDispensaryConfigById (m_model.getPhamDispensaryConfigId (),
                                                                                                       new AsyncCallback <PhamDispensaryConfigModel> ()
                                                                                                       {

                                                                                                           @Override
                                                                                                           public void onFailure (final Throwable caught)
                                                                                                           {
                                                                                                               MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                                 "数据加载失败",
                                                                                                                                 null);
                                                                                                           }

                                                                                                           @Override
                                                                                                           public void onSuccess (final PhamDispensaryConfigModel result)
                                                                                                           {
                                                                                                               m_model = result;
                                                                                                               final OrganizationModel orgModel = new OrganizationModel ();
                                                                                                               orgModel.setCode (result.getPhamDispensaryCode ());
                                                                                                               orgModel.setName (result.getPhamDispensaryName ());
                                                                                                               m_endTime.setSimpleValue (m_model.getEndTime ());
                                                                                                               m_startTime.setSimpleValue (m_model.getStartTime ());
                                                                                                               m_dispensary.setValue (orgModel);
                                                                                                               DictModel dictModel = new DictModel ();
                                                                                                               dictModel.setDictItemCode (result.getPhamType ());
                                                                                                               dictModel.setDictItemName (result.getPhamName ());
                                                                                                               m_phamType.setValue (dictModel);
                                                                                                               dictModel = new DictModel ();
                                                                                                               dictModel.setDictItemCode (result.getShiftType ());
                                                                                                               dictModel.setDictItemName (result.getShiftName ());
                                                                                                               m_shiftType.setValue (dictModel);
                                                                                                               dictModel = new DictModel ();
                                                                                                               dictModel.setDictItemCode (result.getQueueType ());
                                                                                                               dictModel.setDictItemName (result.getQueneName ());
                                                                                                               m_queneAlertType.setValue (dictModel);
                                                                                                               dictModel = new DictModel ();
                                                                                                               dictModel.setDictItemCode (result.getDeliveryType ());
                                                                                                               dictModel.setDictItemName (result.getDeliveryTypeName ());
                                                                                                               m_deliveryType.setValue (dictModel);
                                                                                                               m_prescDate.setValue (result.getPrescDateBefore ());
                                                                                                               initialLeftRadio (result);
                                                                                                               initialRightRadio (result);

                                                                                                           }

                                                                                                       });
        }
    }

    /**
     * 确认或编辑按钮事件
     * 
     * @param model
     */
    private void positiveBtnAction (final PhamDispensaryConfigModel model)
    {
        if (model.getPhamDispensaryConfigId () == null)
        {
            EditPhamDispensaryConfigWindow.s_PHAM_DISPENSARY_CONFIG_ASYNC.addPhamDispensaryConfig (model,
                                                                                                   new AsyncCallback <Void> ()
                                                                                                   {

                                                                                                       @Override
                                                                                                       public void onFailure (final Throwable caught)
                                                                                                       {
                                                                                                           MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                             caught.getMessage (),
                                                                                                                             null);
                                                                                                       }

                                                                                                       @Override
                                                                                                       public void onSuccess (final Void result)
                                                                                                       {
                                                                                                           Info.display (PdsConfigConstant.INFO,
                                                                                                                         "添加药房属性成功！");
                                                                                                           fireEvent (PhamDispensaryConfigModuleEvents.UI_REFRESH);
                                                                                                           if(PhamConfigWidgetUtils.getIsGlobal ()!=0){
                                                                                                               hide ();
                                                                                                           }
                                                                                                           
                                                                                                       }
                                                                                                   });
        }
        else
        {
            EditPhamDispensaryConfigWindow.s_PHAM_DISPENSARY_CONFIG_ASYNC.modifyPhamDispensaryConfig (model,
                                                                                                      new AsyncCallback <Void> ()
                                                                                                      {

                                                                                                          @Override
                                                                                                          public void onFailure (final Throwable caught)
                                                                                                          {
                                                                                                              MessageBox.alert (PdsConfigConstant.ERROR,
                                                                                                                                caught.getMessage (),
                                                                                                                                null);
                                                                                                          }

                                                                                                          @Override
                                                                                                          public void onSuccess (final Void result)
                                                                                                          {
                                                                                                              Info.display (PdsConfigConstant.INFO,
                                                                                                                            "更新药房属性成功成功！");
                                                                                                              fireEvent (PhamDispensaryConfigModuleEvents.UI_REFRESH);
                                                                                                              if(PhamConfigWidgetUtils.getIsGlobal ()!=0){
                                                                                                                  hide ();
                                                                                                              }
                                                                                                          }
                                                                                                      });
        }
    }

    /**
     * 注册监听事件.
     * 
     */
    private void registerListeners ()
    {
        m_cancelButton.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                hide ();
            }
        });
        m_addButton.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final PhamDispensaryConfigModel model = getSaveData ();
                positiveBtnAction (model);
            }
        });
        keyEnterListener ();
        new KeyNav <ComponentEvent> (this)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                if (ce.isAltKey ())
                {
                    if (ce.getKeyCode () == KeysConstants.VK_O)
                    {
                        final PhamDispensaryConfigModel model = getSaveData ();
                        positiveBtnAction (model);
                    }
                    if (ce.getKeyCode () == KeysConstants.VK_C)
                    {
                        hide ();
                    }
                }
            }
        };
    }

	public FormPanel getAddPhamConfigForm() {
		return addPhamConfigForm;
	}

	public Button getAddButton() {
		return m_addButton;
	}
    
}
